home *** CD-ROM | disk | FTP | other *** search
- /*
- WRITE BINARY.c
-
- by: John A. Schlack
- Date: July 12, 1990
-
- This is a procedure designed to help read the files created by Matrix
- Manipulation. It is taken from the source code for Matrix Manipulation
- with certain procedures commented out. This should help one visualize how
- the data should be read as this is the procedure that Matrix Manipulation
- uses to write the matrix data file.
- If you want to know how to write a binary file, see READ BINARY.c.
- You can modify these two files to read and write ASCII files. These are
- only meant as a guide line, and are not meant to be simply placed into
- a program to create and read the Matrix Manipulation files.
- */
-
-
- #define RETURN_CHAR 0x0D
-
- #define TRANSPOSE_FUNC 1
- #define EIGEN_FUNC 2
- #define SOLVE_SYS_FUNC 3
- #define INVERSE_FUNC 4
- #define SET_DEFAULT 5
-
-
- extern struct filePreferences
- {
- Boolean saveASCII;
- } gFilePrefs;
-
- extern double **gA;
- extern double *gD;
- extern int gN;
-
-
- /* -------------------------------------------------------------------- */
-
- void WriteNoHeaderFileData( reply )
-
- SFReply reply;
-
- /*
- WriteASCIILine is a procedure (not provided here) that accepts a string and
- writes the ASCII data of the string to a file
- fileErrors is a procedure to display an alert indicating the type of error
- that occurred; it is not provided here
- gFunc is a variable that stores the type of function written
- gN is the matrix size (N x N)
- gFilePrefs.saveASCII stores a flag which states whether the data should
- be saved in ASCII or binary format
-
- PROCEDURE:
- 1. create a new file
- 2. open the file
- 3. write the number corresponding to the function type (detailed in
- Save File Templates of the included text and #defined above)
- 4. write the size of the matrix (N x N; note: only one value is written)
- 5. depending on the function, write either matrix, vector, or both types
- of data
- 6. set end of file
- 7. close file and flush volume
- */
-
- {
- int srcFile;
- OSErr err;
- Boolean theErr = FALSE;
- long siz;
- double d;
- char tString[256];
-
- if (gFilePrefs.saveASCII == TRUE)
- {
- err = Create( reply.fName, reply.vRefNum, '????', 'TEXT'); /* IS TEXT IN THE RIGHT PLACE ? */
- }
- else
- {
- err = Create( reply.fName, reply.vRefNum, '????', '????');
- }
- if ((err == noErr) || (err == dupFNErr))
- {
- err = FSOpen( reply.fName, reply.vRefNum, &srcFile );
- if (err == noErr)
- {
- /* write function type */
- if (gFilePrefs.saveASCII == TRUE)
- {
- d = (double) gFunc;
- sprintf(tString, "%lf%c\0", d, RETURN_CHAR );
- /* theErr = WriteASCIILine( (char *) tString, srcFile );*/
- }
- else
- {
- siz = (long) sizeof(double);
- d = (double) gFunc;
- err = FSWrite( srcFile, &siz, &d );
- if (err != noErr)
- {
- theErr = TRUE;
- /* fileErrors( err );*/
- }
- }
-
- /* write matrix size */
- if (gFilePrefs.saveASCII == TRUE)
- {
- d = (double) gN;
- sprintf(tString, "%lf%c\0", d, RETURN_CHAR );
- /* theErr = WriteASCIILine( (char *) tString, srcFile );*/
- }
- else
- {
- siz = (long) sizeof(double);
- d = (double) gN;
- err = FSWrite( srcFile, &siz, &d );
- if (err != noErr)
- {
- theErr = TRUE;
- /* fileErrors( err );*/
- }
- }
-
- if (theErr != TRUE)
- {
- switch( gFunc )
- {
- case TRANSPOSE_FUNC :
- case INVERSE_FUNC :
- theErr = WriteFileMatrix( srcFile, gFunc );
- break;
- case EIGEN_FUNC :
- theErr = WriteFileVector( srcFile );
- if (theErr != TRUE)
- {
- theErr = WriteFileMatrix( srcFile, gFunc );
- }
- break;
- case SOLVE_SYS_FUNC :
- theErr = WriteFileVector( srcFile );
- break;
- default:
- break;
- }
- }
-
- GetFPos( srcFile, &siz );
- SetEOF( srcFile, siz );
- FSClose( srcFile );
- FlushVol(0L, reply.vRefNum);
- }
- else
- {
- /* fileErrors(err);*/
- }
- }
- else
- {
- /* fileErrors(err);*/
- }
- }
-
-
- /* ---------------------------------------------------------------- */
-
- Boolean WriteFileVector( srcFile )
-
- int srcFile;
-
- /*
- gD is a global that stores the solution vector (array [0..N])
-
- PROCEDURE:
- 1. loop through from 1 to N and write vector values to disk
- */
-
- {
- OSErr err = noErr;
- Boolean theErr = FALSE;
- register int i = 1;
- long siz;
- char tString[256];
- char d;
-
- while ((err == noErr) && (i <= gN))
- {
- i += 1;
- if (gFilePrefs.saveASCII != TRUE)
- {
- siz = (long) sizeof(double);
- err = FSWrite( srcFile, &siz, &(gD[i]) );
- }
- else
- {
- sprintf(tString, "%lf%c\0", gD[i], RETURN_CHAR );
- /* theErr = WriteASCIILine( (char *) tString, srcFile );*/
- if (theErr == TRUE)
- {
- i = gN + 1;
- }
- }
- }
- if (err != noErr)
- {
- /* fileErrors( err );*/
- theErr = TRUE;
- }
- return( theErr );
- }
-
-
- /* ---------------------------------------------------------------- */
-
- Boolean WriteFileMatrix( srcFile, theType )
-
- int srcFile;
- int theType;
-
- /*
- gA is a global double ** that stores the matrix (it is an array [0..N][0..N])
-
- PROCEDURE:
- 1. loop through from 1 to N for row with 1 to N for column nested in the original
- loop and write matrix values to disk
- */
-
- {
- OSErr err;
- Boolean theErr = FALSE;
- register int i;
- register int j;
- long siz;
- char tString[256];
- char d;
-
- if (gFilePrefs.saveASCII == TRUE)
- {
- if (gN <= 9)
- {
- d = '\t';
- }
- else
- {
- d = RETURN_CHAR;
- }
- }
-
- for (i=1; i<=gN; i++)
- {
- for (j=1; j<=gN; j++)
- {
- if (gFilePrefs.saveASCII != TRUE)
- {
- siz = (long) sizeof(double);
- if (theType == EIGEN_FUNC)
- {
- err = FSWrite( srcFile, &siz, &(gA[j][i]) ); /* eigenvectors are stored as columns for a particular eigenvalue, not as rows */
- }
- else
- {
- err = FSWrite( srcFile, &siz, &(gA[i][j]) );
- }
- if (err != noErr)
- {
- /* fileErrors( err );*/
- theErr = TRUE;
- j = gN + 1;
- i = gN + 1;
- }
- }
- else
- {
- if (theType == EIGEN_FUNC)
- {
- sprintf(tString, "%lf%c\0", gA[j][i], d );
- /* theErr = WriteASCIILine( (char *) tString, srcFile );*/
- if (theErr == TRUE)
- {
- i = gN + 1;
- j = gN + 1;
- }
- }
- else
- {
- sprintf(tString, "%lf%c\0", gA[i][j], d );
- /* theErr = WriteASCIILine( (char *) tString, srcFile );*/
- if (theErr == TRUE)
- {
- i = gN + 1;
- j = gN + 1;
- }
- }
- }
- if ((gFilePrefs.saveASCII == TRUE) && (gN <= 9))
- {
- sprintf(tString, "%c\0", RETURN_CHAR );
- /* theErr = WriteASCIILine( (char *) tString, srcFile );*/
- if (theErr == TRUE)
- {
- i = gN + 1;
- j = gN + 1;
- }
- }
- }
- }
- return( theErr );
- }
-